﻿using System;
/*Write a program that finds the index of given element in a sorted array of integers by using the 
 * binary search algorithm (find it in Wikipedia).
*/


    class BinarySearch
    {
        static void Main()
        {
            int[] array = new int[] { 1, 4, 7, 8, 10, 12, 16, 17 };

            int number = 13;
            int i = 0;
            int j = array.Length - 1;
            int middle = 0;

            while (i != j)
            {
                middle = (j + i) / 2;
                
                if (array[middle] >= number)
                {
                    j = middle;
                }
                else if (array[middle] <= number)
                {
                    i = middle+1;
                }
            }
            if (array[i] == number)
            {
                Console.WriteLine("The number matches at position {0}!", i);

            }
            else
            {
                Console.WriteLine("Number not found");
            }
        }
    }

